package com.jyx.search.controller;

import com.jyx.api.search.EsCourseControllerApi;
import com.jyx.framework.domain.course.CoursePub;
import com.jyx.framework.domain.course.TeachplanMediaPub;
import com.jyx.framework.domain.search.CourseSearchParam;
import com.jyx.framework.model.response.QueryResponseResult;
import com.jyx.framework.model.response.QueryResult;
import com.jyx.search.service.EsCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/search/course")
public class EsCourseController implements EsCourseControllerApi {
    @Autowired
    private EsCourseService esCourseService;

    @Override
    @GetMapping(value = "/list/{page}/{size}")
    public QueryResponseResult<CoursePub> list(@PathVariable("page") int page, @PathVariable("size") int size,
                                               CourseSearchParam courseSearchParam) throws Exception {
        return esCourseService.list(page,size,courseSearchParam);
    }

    @Override
    @GetMapping("/getall/{id}")
    public Map<String, CoursePub> getAll(@PathVariable("id") String id) {
        return esCourseService.getAll(id);
    }

    @Override
    @GetMapping("/getmedia/{teachplanId}")
    public TeachplanMediaPub getMedia(@PathVariable("teachplanId") String teachplanId) {
        //将课程计划id放在数组中，为调用
        String[] teachplanIds = new String[]{teachplanId};
        QueryResponseResult<TeachplanMediaPub> mediaPubQueryResponseResult = esCourseService.getMedia(teachplanIds);
        QueryResult<TeachplanMediaPub> queryResult = mediaPubQueryResponseResult.getQueryResult();
        if(queryResult != null && queryResult.getList()!=null && queryResult.getList().size()>0){
            return queryResult.getList().get(0);
        }
        return new TeachplanMediaPub();

    }
}
